package edu.ncu.dao;

import edu.ncu.entity.Comment;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface CommentDao {

    /**
     * int commentId;
     * int commentType;
     * int commentObjectId
     * String content;
     * Date commentTime;
     * int likeNum;
     * int commenterId;
     * int cNum ;  // 评论所拥
     * 根据动态id查询所有的评论
     * 动态的类型为0
     * @param
     * @return
     */
    @Select("select * from publish_comment where publishId=#{publishId}")
//    @Results(id="cMap",value = {
//            @Result(id = true,property = "commentId",column = "cid"),
//            @Result(property = "commentType",column = "type"),
//            @Result(property = "commentObjectId",column = "case_id"),
//            @Result(property = "content",column = "content"),
//            @Result(property = "commentTime",column = "cTime"),
//            @Result(property = "likeNum",column = "likesNum"),
//            @Result(property = "commenterId",column = "user_hold"),
//            @Result(property = "cNum",column = "num")
//    })
    @Results(id="publish_comment",value = {
            @Result(id = true,property = "commentId",column = "id"),
            @Result(property = "content",column = "content"),
            @Result(property = "commentTime",column = "commentTime"),
            @Result(property = "likeNum",column = "likeNum"),
            @Result(property = "commenterId",column = "commenterId"),
            @Result(property = "beCommented",column = "publishId"),
            @Result(property = "parentId",column = "parentId")
    })
    List<Comment> findPublishComment(int publishId) ;

    @Select("select * from photo_comment where photoId=#{photoId}")
    @Results(id="photo_comment",value = {
            @Result(id = true,property = "commentId",column = "id"),
            @Result(property = "content",column = "content"),
            @Result(property = "commentTime",column = "commentTime"),
            @Result(property = "likeNum",column = "likeNum"),
            @Result(property = "commenterId",column = "commenterId"),
            @Result(property = "beCommented",column = "photoId"),
            @Result(property = "parentId",column = "parentId")
    })
    List<Comment> findPhotoComment(int photoId) ;

    /**动态
     * 根据评论的id(parentId)查询他所有的子评论
     * @param commentId
     * @return
     */
    @Select("select * from publish_comment where parentId=#{commentId}")
    @ResultMap("publish_comment")
    List<Comment> findChildCommentOfPublish(int commentId) ;
    /**照片
     * 根据评论的id查询他所有的子评论
     * @param commentId
     * @return
     */
    @Select("select * from photo_comment where parentId=#{commentId}")
    @ResultMap("photo_comment")
    List<Comment> findChildCommentOfPhoto(int commentId) ;
    @Select("select * from publish_comment where id=#{commentId}")
    @ResultMap("publish_comment")
    Comment findCommentInPublish(int commentId) ;
    @Select("select * from photo_comment where id=#{commentId}")
    @ResultMap("photo_comment")
    Comment findCommentInPhoto(int commentId) ;



    /**
     * 根据照片的id查询照片对应的所有评论
     * @param photoId
     * @return
     */
    @Select("select * from photo_comment where photoId=#{photoId}")
    @ResultMap("photo_comment")
    List<Comment> findCommentByPhoto(int photoId) ;
    /**
     * 根据照片的id查询照片对应的所有评论
     * @param publishId
     * @return
     */
    @Select("select * from publish_comment where publishId=#{publishId}")
    @ResultMap("publish_comment")
    List<Comment> findCommentByPublish(int publishId) ;



    /**
     * 首先在插入的时候检查插入对象是谁
     * 如果是评论，则将type置为-1，表示不能再插入评论的评论
     * 也就是说评论只有两级评论
     *
     * @param c
     * @return
     */
    @Insert("insert into publish_comment(content,commentTime,likeNum,commenterId,publishId,parentId) " +
            " values(#{content}, #{commentTime}, #{likeNum}, #{commenterId}, #{publishId} ,#{parentId})")
    boolean insertPublishComment(Comment c) ;

    @Insert("insert into photo_comment(content,commentTime,likeNum,commenterId,photoId,parentId) " +
            " values(#{content}, #{commentTime}, #{likeNum}, #{commenterId}, #{photoId} ,#{parentId})")
    boolean insertPhotoComment(Comment c) ;


    /**
     * 根据评论的id删除评论
     * @param commentId
     * @return
     */
    @Delete("delete from publish_comment where id=#{commentId}")
    boolean deletePublishComment(int commentId) ;
    @Delete("delete from photo_comment where id=#{commentId}")
    boolean deletePhotoComment(int commentId) ;

    /**
     * 根据评论id查询评论
     * @param commentId
     * @return
     */
    @Select("select * from publish_comment where id=#{commentId}")
    @ResultMap("publish_comment")
    Comment getPublishCommentById(int commentId) ;

    @Select("select * from photo_comment where id=#{commentId}")
    @ResultMap("photo_comment")
    Comment getPhotoCommentById(int commentId) ;


//    /**
//     * 更新子评论数量
//     * @param commentId
//     * @return
//     */
//    @Update("update discuss_1 set num=num+1 where cid=#{commentId}")
//    boolean updateCommentNum(int commentId) ;
//
    /**
     * 更新评论点赞数量
     * @param commentId
     * @return
     */
    @Update("update publish_comment set likeNum=likeNum+1 where id=#{commentId}")
    boolean updatePublishCommentGreat(int commentId) ;
    @Update("update photo_comment set likeNum=likeNum+1 where id=#{commentId}")
    boolean updatePhotoCommentGreat(int commentId) ;

}
